package com.b1502.store2.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.b1502.store2.R;
import com.b1502.store2.model.Product;
import com.bumptech.glide.Glide;

import java.util.List;

/**
 * 1. 主页产品列表适配器
 * 2. @author：liqingyi
 * 3. @date：2017/2/27 14:22
 */

public class HomeProductAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

   private List<Product> list;
   private Context context;
    private RecyItemClickListener recyItemClickListener;
    public void setRecyItemClickListener(RecyItemClickListener recyItemClickListener) {
        this.recyItemClickListener = recyItemClickListener;
    }

    public HomeProductAdapter(Context context, List<Product> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        if(viewType == 0){
             view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_home_product, parent, false);
        }
        return new ProductHolder(view);
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof ProductHolder){
            Glide.with(context).load(list.get(position).getImagePath()).into(((ProductHolder) holder).home_product_image);
            ((ProductHolder) holder).home_product_name.setText(list.get(position).getName());
            ((ProductHolder) holder).home_product_price.setText("¥"+list.get(position).getPrice());
            if("Given|Reduce".equals(list.get(position).getPromotionLabel())){
                ((ProductHolder) holder).home_text1.setVisibility(View.VISIBLE);
                ((ProductHolder) holder).home_text2.setVisibility(View.VISIBLE);
            }
            if("Reduce".equals(list.get(position).getPromotionLabel())){
                ((ProductHolder) holder).home_text2.setVisibility(View.VISIBLE);
            }
            if("Given".equals(list.get(position).getPromotionLabel())){
                ((ProductHolder) holder).home_text1.setVisibility(View.VISIBLE);
            }
        }
        //item点击事件
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int layoutPosition = holder.getLayoutPosition();
                    recyItemClickListener.addRecyItemClickListener(layoutPosition);
                }
            });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public int getItemViewType(int position) {
        return 0;
    }

    static class ProductHolder extends RecyclerView.ViewHolder {

        private final ImageView home_product_image;
        private final TextView home_product_name,home_product_price,home_text1,home_text2;

        public ProductHolder(View itemView) {
            super(itemView);
            home_product_image = (ImageView) itemView.findViewById(R.id.home_product_image);
            home_product_name = (TextView) itemView.findViewById(R.id.home_product_name);
            home_product_price = (TextView) itemView.findViewById(R.id.home_product_price);
            home_text1 = (TextView) itemView.findViewById(R.id.home_text1);
            home_text2 = (TextView) itemView.findViewById(R.id.home_text2);
        }
    }
    //定义recyclerView条目监听
    public interface RecyItemClickListener{
        void addRecyItemClickListener(int pos);
    }

}
